package org.netty.buffers;

import java.util.Iterator;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.Unpooled;

/**
 * 复合缓冲区
 * Description:
 * @author zpp
 * @date   2018年5月9日
 */
public class CompositeByteBufTest {
	
	public static void main(String[] args) {
		CompositeByteBuf comBuf = Unpooled.compositeBuffer();
		
		ByteBuf heapBuf = Unpooled.buffer(8);
		ByteBuf directBuf = Unpooled.directBuffer(16);
		heapBuf.writeByte(10);
		directBuf.writeByte(10);
		
		comBuf.addComponents(heapBuf, directBuf);
		//删除第一个ByteBuf
		comBuf.removeComponent(0);
		
		Iterator<ByteBuf> iter = comBuf.iterator();
		while (iter.hasNext()) {
			System.out.println(iter.next().readableBytes());
		}
		// 使用数组访问数据
		if (!comBuf.hasArray()) {
			int len = comBuf.readableBytes();
			byte[] arr = new byte[len];
			comBuf.getBytes(0, arr);
		}
	}
}
